package ${package.Controller};

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import ${package.Controller}.swagger.${table.controllerName}Swagger;
import ${package.Service}.${table.serviceName};
import ${package.Entity}.${entity};
import com.youan.pangu.base.util.PrimaryKeyUtil;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;
import org.springframework.beans.factory.annotation.Autowired;



<#if superControllerClassPackage??>
import ${superControllerClassPackage};
</#if>

import java.util.List;

/**
 * <p>
 * ${table.comment!} 前端控制器
 * </p>
 *
 * @author ${author}
 * @since ${date}
 */
@Controller
@RequestMapping("/<#if controllerMappingHyphenStyle??>${controllerMappingHyphen}<#else>${entity?uncap_first}</#if>")
<#if superControllerClass??>
public class ${table.controllerName} extends ${superControllerClass} implements ${table.controllerName}Swagger {
<#else>
public class ${table.controllerName} implements ${table.controllerName}Swagger{
</#if>
    @Autowired
    private ${table.serviceName} ${entity?uncap_first}Service;

<#list table.fields as field>
    <#if field.keyFlag>
    @Override
    @PostMapping("/${entity?uncap_first}")
    public ${entity} add${entity}(${entity} ${entity?uncap_first}){
        ${entity?uncap_first}.set${field.propertyName?cap_first}(PrimaryKeyUtil.getCommonId());
        ${entity?uncap_first}Service.save(${entity?uncap_first});
        return ${entity?uncap_first};
    }

    @Override
    @DeleteMapping("/${entity?uncap_first}/{${field.propertyName}}")
    public void delete${entity}By${field.propertyName?cap_first}(@PathVariable ${field.propertyType} ${field.propertyName}){
        ${entity?uncap_first}Service.removeById(${field.propertyName});
    }

    @Override
    @GetMapping("/${entity?uncap_first}/{${field.propertyName}}")
    public ${entity} select${entity}By${field.propertyName?cap_first}(@PathVariable ${field.propertyType} ${field.propertyName}){
        return ${entity?uncap_first}Service.getById(${field.propertyName});
    }
    </#if>
</#list>

    @Override
    @PutMapping("/${entity?uncap_first}")
    public ${entity} update${entity}(${entity} ${entity?uncap_first}){
        ${entity?uncap_first}Service.updateById(${entity?uncap_first});
        return ${entity?uncap_first};
    }

    @Override
    @GetMapping("/${entity?uncap_first}s")
    public List<${entity}> selectAll${entity}(){
        return ${entity?uncap_first}Service.list();
    }

    @Override
    @GetMapping("/${entity?uncap_first}s/example")
    public List<${entity}> select${entity}sByExample(${entity} ${entity?uncap_first}) {
        QueryWrapper<${entity}> wrapper = new QueryWrapper<>(${entity?uncap_first});
        return ${entity?uncap_first}Service.list(wrapper);
    }

    @Override
    @GetMapping("/${entity?uncap_first}s/pageable")
    public Page<${entity}> select${entity}Pageable(Page page){
        return ${entity?uncap_first}Service.page(page);
    }

    @Override
    @GetMapping("/${entity?uncap_first}s/example/pageable")
    public Page<${entity}> select${entity}sByExamplePageable(Page<${entity}> page, ${entity} ${entity?uncap_first}) {
        QueryWrapper<${entity}> wrapper = new QueryWrapper<>(${entity?uncap_first});
        return ${entity?uncap_first}Service.page(page, wrapper);
    }
}

